package cn.seocoo.platform.common.utils;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

/**
 * @author seocoo
 * @ClassName: SendMailUtils
 * @Description: 发送Email
 */
@Component
public class SendMailUtils {




    private static String host;
    @Value("${para.email.host}")
    public void setHost(String host) {
        SendMailUtils.host = host;
    }


    private static String protocol;
    @Value("${para.email.protocol}")
    public void setProtocol(String protocol) {
        SendMailUtils.protocol = protocol;
    }


    private static String port;
    @Value("${para.email.port}")
    public void setPort(String port) {
        SendMailUtils.port = port;
    }


    private static String auth;
    @Value("${para.email.auth}")
    public void setAuth(String auth) {
        SendMailUtils.auth = auth;
    }

    private static String name;
    @Value("${para.email.name}")
    public void setName(String name) {
        SendMailUtils.name = name;
    }

    private static String password;
    @Value("${para.email.password}")
    public void setPassword(String password) {
        SendMailUtils.password = password;
    }

    /**
     * @param receiverList
     * @param subject
     * @throws Exception
     */
    public static void main(List<String> receiverList, String subject) throws Exception {
        Properties prop = new Properties();
        prop.setProperty("mail.host", host);
        prop.setProperty("mail.transport.protocol", protocol);
        prop.setProperty("mail.smtp.port", port);
        prop.setProperty("mail.smtp.auth", auth);
        prop.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        prop.setProperty("mail.smtp.socketFactory.fallback", "false");
        prop.setProperty("mail.smtp.socketFactory.port", port);
        //使用JavaMail发送邮件的5个步骤
        //1、创建session
        Session session = Session.getInstance(prop);
        //开启Session的debug模式，这样就可以查看到程序发送Email的运行状态
        session.setDebug(true);
        //2、通过session得到transport对象
        Transport transport = session.getTransport();
        //3、连上邮件服务器   kvuufahoyvjgdjcc gkzjxguevsbkcecj
        //imap hulephcdwoztfcgf
        transport.connect(host, name, password);
//        List<String> receiverList = new ArrayList<>();
//        receiverList.add("983152468@qq.com");
//        receiverList.add(email);
//        String subject = "测试测试";
//      附件
        List<String> filePathList = new ArrayList<>(2);
//        filePathList.add("G:\\Downloads\\xszf.png");
//        filePathList.add("C:\\Users\\Administrator\\Desktop\\pic3.png");
        //4、创建邮件
        Message message = createAttachMail(session, name, receiverList, subject, filePathList);
        //5、发送邮件
        transport.sendMessage(message, message.getAllRecipients());
        transport.close();
    }

    /**
     * @param session
     * @param mailFrom     发件人
     * @param receiverList 收件人的集合
     * @param subject      邮件标题
     * @param filePathList 附件的路径的集合
     * @return
     * @throws Exception
     */
    public static MimeMessage createAttachMail(Session session, String mailFrom, List<String> receiverList, String subject, List<String> filePathList) throws Exception {
        MimeMessage message = new MimeMessage(session);
        //设置邮件的基本信息
        //发件人
        message.setFrom(new InternetAddress(mailFrom));
        //收件人
        InternetAddress[] addresseses = new InternetAddress[receiverList.size()];
        for (int i = 0; i < receiverList.size(); i++) {
            addresseses[i] = new InternetAddress(receiverList.get(i), receiverList.get(i), "UTF-8");
        }
        message.setRecipients(Message.RecipientType.TO, addresseses);
//        message.setRecipient(Message.RecipientType.TO, new InternetAddress("wangpan03@126.com"));
        //邮件标题
        message.setSubject(subject);
        //创建邮件正文，为了避免邮件正文中文乱码问题，需要使用charset=UTF-8指明字符编码
        MimeBodyPart text = new MimeBodyPart();
        text.setContent("<h1>" + subject + "</h1>", "text/html;charset=UTF-8");
        //创建容器描述数据关系
        MimeMultipart mp = new MimeMultipart();
        //暂时不展示正文内容
        mp.addBodyPart(text);
        //创建邮件附件
        MimeBodyPart attach = null;
        DataHandler dh = null;
        for (String filePath : filePathList) {
            attach = new MimeBodyPart();
            dh = new DataHandler(new FileDataSource(filePath));
            attach.setDataHandler(dh);
            attach.setFileName(dh.getName());
            //添加多个附件
            mp.addBodyPart(attach);
        }
        mp.setSubType("mixed");

        message.setContent(mp);
        message.saveChanges();
        //将创建的Email写入到E盘存储
//        message.writeTo(new FileOutputStream("E:\\attachMail.eml"));
        //返回生成的邮件
        return message;
    }
}
